#include <16F877A.H>
#include<stdio.h>
#include<stdlib.h>

//#define RTC PIN_B7
//#define MENU PIN_B6
//#define MENU PIN_B6

/*========================================== Controller Configuration Settings========================================*/
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
unsigned char temp_a=0,temp_b=0,pressure=0,load=100;
unsigned char sec,min,hour,temp,valu;

//#define EEPROM_SDA  PIN_C4
//#define EEPROM_SCL  PIN_C3

#include <24256.c>
#include <rtc.h>

//=====================================
void main()
{
unsigned char hour1,min1,sec1;
delay_ms(300);
printf("\n\n\r                             BLACK BOX   \n\r");
Printf("____________________________________________________________\n\r");
//printf(" TIME       TEMPERATURE1   TEMPERATURE1   LOADCELL \n\r");

   port_b_pullups(true);

   init_ext_eeprom();
   setup_port_A(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
    RTC_Init();          // initial DS1307
  // printf("RTC init done");
   sec=RTC_Read(0);
   RTC_Write(0,sec & 0x7F); // enable oscillator(bit 7 =0)
   RTC_RD();
   temp = sec;
   delay_ms(2000);
   RTC_RD();
   delay_ms(100);
   if(temp==sec)
   {
      printf("Error in RTC");
   }
while(1)
{valu=0;
      printf("\n\rEnter 1.Serive Menu 2.Monitor 3.Last Stored Value");
      do {

   valu=getch();

   }while(valu!='3' && valu!='1' && valu!='2');

//      delay_ms(500);
         //  valu=getchar();
          //  Printf("%d",valu);
      if(valu=='1')
      {

            Printf("\n\r*******Service Menu*******");
            Printf("\n\rEnter Hour ");
            hour = getch();
            Printf("%d",(hour-0x30));
            hour1 = getch();
            Printf("%d",(hour1-0x30));
            Printf("\n\rEnter Minute");
            min = getch();
            Printf("%d",(min-0x30));
            min1 = getch();
            Printf("%d",(min1-0x30));
            Printf("\n\rEnter Second");
            sec  = getch();
            Printf("%d",(sec-0x30));
            sec1  = getch();
            Printf("%d",(sec1-0x30));
            DS1307_settime((((hour-0x30)*10)+(hour1-0x30)),(((min-0x30)*10)+(min1-0x30)),(((sec-0x30)*10)+(sec1-0x30)));
      }
       if(valu=='2')
      {

Printf("\n\n\r----------------------------------------------------------\n");
printf("\r TIME       TEMPERATURE1   TEMPERATURE2   LOADCELL \n");
Printf("\n\n\r----------------------------------------------------------\n");

   //while(load>96)
   while(1)
   {

   RTC_RD();
   printf("\r%x:%x:%x     ",hour,min,sec);

      write_ext_eeprom(4,hour);
      write_ext_eeprom(5,min);
      write_ext_eeprom(6,sec);

      set_adc_channel(0);
      delay_us(10);
      temp_a=Read_ADC();
      delay_ms(300);
      printf("    %u      ",temp_a);
      write_ext_eeprom(0,temp_a);
      delay_ms(300);

      set_adc_channel(1);
      delay_us(10);
      temp_b=Read_ADC();
      delay_ms(300);
      printf("          %u    ",temp_b);
      write_ext_eeprom(1,temp_b);
      delay_ms(300);

      /*set_adc_channel(2);
      delay_us(10);
      pressure=Read_ADC();
      printf("Pressure=%u\n",pressure);
      write_ext_eeprom(2,pressure);
      delay_ms(300);*/

      set_adc_channel(3);
      delay_us(10);
      load=Read_ADC();
      printf("      %u  \n\r",load);
     /* if(load<85)
      {
      printf("over load=%u\n",load);
      }*/
      delay_ms(300);
   }
      }
      else if(valu=='3')
      {
Printf("\n\n\r                      LAST VALUES ARE\n\r");
Printf("=============================================================\n");
printf("\rTemperature1  %d \n", read_ext_eeprom(0));
printf("\rTemperature2 %d \n", read_ext_eeprom(1));
printf("\rTime %x:%x:%x \n", read_ext_eeprom(4), read_ext_eeprom(5), read_ext_eeprom(6));
//printf(" %c  %d \n", read_ext_eeprom(2), read_ext_eeprom(2));
Printf("\r\n=============================================================\n");
      }
      else
      {
      Printf("Enter Valid Number");
      }

      delay_ms(500);
//while(1);
}
}
